﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace METU_CMT.Business.Report
{
    public class ReportGenerator
    {
        private List<ReportInfo> reportInfos = new List<ReportInfo>();
        private string courseName;

        public List<ReportInfo> ReportInfos
        {
            get { return reportInfos; }
            set { reportInfos = value; }
        }

        public string CourseName
        {
            get { return courseName; }
            set { courseName = value; }
        }

        public void AddReportInfo(ReportInfo reportInfo)
        {
            ReportInfos.Add(reportInfo);
        }

        public void CreateReportAndDownload()
        {
            StreamWriter _streamWriter =
                new StreamWriter(HttpContext.Current.Server.MapPath("/") + "SubmissionReport.html");
            _streamWriter.WriteLine("<html>");
            _streamWriter.WriteLine("<head>");
            _streamWriter.WriteLine("<META http-equiv=content-type content=text/html;charset=windows-1254>");
            _streamWriter.WriteLine("<title>Submission Report</title>");
            _streamWriter.WriteLine("</head>");
            _streamWriter.WriteLine("<body>");
            _streamWriter.WriteLine("<h1 style=\"text-align: center;\">");
            _streamWriter.WriteLine(courseName + "</h1>");
            _streamWriter.WriteLine("<h2 style=\"text-align: center;\">");
            _streamWriter.WriteLine("SUBMISSION REPORT</h2>");
            _streamWriter.WriteLine("<hr />");
            _streamWriter.WriteLine("		<p style=\"font-size:10px;\">");
            _streamWriter.WriteLine("&nbsp;</p>");
            _streamWriter.WriteLine("		<table align=\"center\" border=\"2\" cellpadding=\"1\" cellspacing=\"1\" style=\"width: 600px;\">");
            _streamWriter.WriteLine("<thead>");
            _streamWriter.WriteLine("<tr>");
            _streamWriter.WriteLine("<th scope=\"col\">");
            _streamWriter.WriteLine("Submission Name</th>");
            _streamWriter.WriteLine("<th scope=\"col\">");
            _streamWriter.WriteLine("Document Name</th>");
            _streamWriter.WriteLine("<th scope=\"col\">");
            _streamWriter.WriteLine("Team Name</th>");
            _streamWriter.WriteLine("<th scope=\"col\">");
            _streamWriter.WriteLine("Submitted Date</th>");
            _streamWriter.WriteLine("</tr>");
            _streamWriter.WriteLine("<thead>");
            _streamWriter.WriteLine("<caption>");
            _streamWriter.WriteLine("&nbsp;</caption>");
            _streamWriter.WriteLine("<tbody>");
            foreach (ReportInfo _reportInfo in ReportInfos)
            {
                _streamWriter.WriteLine("<tr>");
                _streamWriter.WriteLine("<td>");
                _streamWriter.WriteLine("<span style=\"color: black;\">" + _reportInfo.Deliverable.Event.Description + "</span></td>");
                _streamWriter.WriteLine("<td>");
                _streamWriter.WriteLine(_reportInfo.Deliverable.File + "</td>");
                _streamWriter.WriteLine("<td>");
                _streamWriter.WriteLine(_reportInfo.Team.TeamName + "</td>");
                _streamWriter.WriteLine("<td>");
                if (string.IsNullOrEmpty(_reportInfo.Deliverable.File) == true)
                {
                    _streamWriter.WriteLine(
                    "<span style=\"font-family: &quot;Arial&quot;,&quot;sans-serif&quot;; color: black;\">" +
                    string.Empty + "</span></td>");
                }
                else
                {
                    _streamWriter.WriteLine(
                    "<span style=\"font-family: &quot;Arial&quot;,&quot;sans-serif&quot;; color: black;\">" +
                    _reportInfo.Deliverable.UploadTime + "</span></td>");
                }
                _streamWriter.WriteLine("</tr>");
            }
            _streamWriter.WriteLine("</tbody>");
            _streamWriter.WriteLine("</table>");
            _streamWriter.WriteLine("<br />");
            _streamWriter.WriteLine("<br />");
            _streamWriter.WriteLine("<hr />");
            _streamWriter.WriteLine("</body>");
            _streamWriter.WriteLine("</html>");
            _streamWriter.Close();

            CommonMethods.DownloadFile(HttpContext.Current.Response,
                                       HttpContext.Current.Server.MapPath("/") + "SubmissionReport.html", FileType.NA);
        }

    }
}